﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using TestesFixacao.Core.Extensions;
using TestesFixacao.Dominio.Modelos;

namespace TestesFixacao.Dominio.Readers
{
    public class DisciplinaReader : IConvertible<List<Disciplina>>
    {
        private QuestaoReader _questaoReader;

        public DisciplinaReader(QuestaoReader questaoReader)
        {
            _questaoReader = questaoReader;
        }

        public void Convert(XmlTextReader readerRoot, List<Disciplina> disciplinas)
        {
            int numeroDisciplinas = 0;

            var readerDisciplinas = readerRoot.ElementCount("disciplinas", "disciplina", ref numeroDisciplinas);

            readerDisciplinas.ReadStartElement("disciplinas");

            for (int i = 0; i < numeroDisciplinas; i++)
            {
                var item = new Disciplina();

                readerDisciplinas.ReadStartElement("disciplina");

                item.Nome = readerDisciplinas.ReadElementString("nome");

                item.Questoes = new List<Questao>();

                _questaoReader.Convert(readerDisciplinas, item.Questoes);

                readerDisciplinas.ReadEndElement();

                disciplinas.Add(item);
            }

            readerDisciplinas.ReadEndElement();
        }
    }
}